package com.bloody.android;

import java.util.HashMap;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.bloody.android.R;
import com.bloody.android.Characters.Character;

public class AbilityShow extends PlayerWindowInteraction {

	private TextView nameAbility;
	private TextView life;
	private ImageView img;
	private static HashMap<String, Integer> h=null;
	private String[] characters;
	private boolean timeTouch=false;
	private String giverID, receiverID;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.ability);
		Bundle bundle = this.getIntent().getExtras();

		int animation = bundle.getInt("animation");
		
		nameAbility = (TextView)findViewById(R.id.abilityName);
		nameAbility.setText(bundle.getString("name")+ " - Level "+(animation+1)+" !");
		//Bundle info = this.getIntent().getExtras();

		//int animation = bundle.getInt("animation");

		Character giver = (Character)bundle.getSerializable("giver");
		Character receiver = (Character)bundle.getSerializable("receiver");
		this.giverID = giver.getID();
		this.receiverID = receiver.getID();
		if(h==null)
		{
			h = new HashMap<String, Integer>();
			h.put("Ichigo_giver0", R.layout.getsuga_animation);
			h.put("Ichigo_giver1", R.layout.getsuga_animation);
			h.put("Ichigo_giver2", R.layout.getsuga_animation);
			h.put("Ichigo_receiver", R.layout.ichigo_hurt_animation);
			h.put("Naruto_giver0", R.layout.coup_naruto_animation);
			h.put("Naruto_giver1", R.layout.rasengan_animation);
			h.put("Naruto_giver2", R.layout.rasen_shuriken_animation);
			h.put("Naruto_receiver", R.layout.naruto_hurt_animation);
			h.put("Gaara_giver0", R.layout.gaara_hit0_animation);
			h.put("Gaara_giver1", R.layout.gaara_hit1_animation);
			h.put("Gaara_giver2", R.layout.gaara_hit2_animation);
			h.put("Gaara_receiver", R.layout.gaara_hurt_animation);
			h.put("Mustang_giver0", R.layout.mustang_hit0_animation);
			h.put("Mustang_giver1", R.layout.mustang_hit1_animation);
			h.put("Mustang_giver2", R.layout.mustang_hit2_animation);
			h.put("Mustang_receiver", R.layout.mustang_hurt_animation);
			h.put("Kakashi_giver0", R.layout.kakashi_hit0_animation);
			h.put("Kakashi_giver1", R.layout.kakashi_hit1_animation);
			h.put("Kakashi_giver2", R.layout.kakashi_hit2_animation);
			h.put("Kakashi_receiver", R.layout.kakashi_hurt_animation);
		}

		String[] tmpName1 = giver.getClass().getName().split("\\.");
		String[] tmpName2 = receiver.getClass().getName().split("\\.");
		characters = new String[2];
		characters[0]= tmpName1[tmpName1.length-1]+"_giver"+animation;
		characters[1]= tmpName2[tmpName2.length-1]+"_receiver";
		life = (TextView)findViewById(R.id.life);
		life.setText("life : " + receiver.getLife());

	}

	public void onWindowFocusChanged(boolean hasFocus) {
		img = (ImageView)findViewById(R.id.img);
		img.setBackgroundResource(h.get(characters[0]));

		AnimationDrawable imgAnimation = (AnimationDrawable) img.getBackground();
		if (hasFocus) {
			imgAnimation.start();
		} else {
			imgAnimation.stop();
		}
	} 

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction()==MotionEvent.ACTION_UP){
			if (!timeTouch){
				img.setBackgroundResource(h.get(characters[1]));

				AnimationDrawable imgAnimation = (AnimationDrawable) img.getBackground();
				imgAnimation.start();
				timeTouch=true;
				nameAbility.setVisibility(4);
				life.setVisibility(0);
			}
			else{
				Bundle bundle = new Bundle();
				bundle.putString("giverID", this.giverID);
				bundle.putString("receiverID", this.receiverID);
				
				Intent mIntent = new Intent();
				mIntent.putExtras(bundle);
				setResult(BloodyCrusadeGame.RES_ENDABILITY, mIntent);
				finish();
			}
		}
		return super.onTouchEvent(event);
	}

}
